<?php
namespace Models;
class CommentModel extends CommonModel {
    public function add($data) {
        $sql = 'insert into comments(post_id, user_id, content) values (:post_id, :user_id, :content)';
        $stmt = $this->db->prepare($sql);
        return $stmt->execute([':post_id' => $data['post_id'], ':user_id' => $data['user_id'], ':content' => $data['content']]);
    }

    public function getCommentByPostId($post_id) {
        $sql = 'select comments.id, comments.content, comments.created_at, comments.praise_count, comments.step_count, users.username from comments
                left join users on users.id = comments.user_id where comments.post_id = ' . $post_id . ' order by comments.id desc';
        return $this->db->query($sql)->fetchAll();
    }

    public function evaluate($type, $comment_id) {
        if ($type == 1) {
            $sql = 'update comments set praise_count = praise_count + 1 where id = ' . $comment_id;
        } else {
            $sql = 'update comments set step_count = step_count + 1 where id = ' . $comment_id;
        }
        return $this->db->exec($sql);
    }
}